
package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.RecDishListAdapter;
import com.afd.ozblip.entity.RecDishEntity;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class RecommendDishActivity extends BaseActivity {

    /****************** 视图变量 **************************/
    private LinearLayout addRecommendDish;
    private ListView recDishListView;
    /****************** 其它变量 **************************/
    private List<RecDishEntity> recDishList;
    private RecDishListAdapter recDishListAdapter;

    private String businessId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recommend_dish);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
        // 获取列表数据
        requestDishListData();
    }

    /**
     * 寻找视图
     */
    public void findViews() {
        this.addRecommendDish = (LinearLayout) findViewById(R.id.ll_add_recommendDish);
        this.recDishListView = (ListView) findViewById(R.id.lv_business_recommend_dishList);
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener recommendListClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.ll_business_detail_recommendDish:
                        Intent settingIntent = new Intent(
                                RecommendDishActivity.this,
                                DishDetailActivity.class);
                        startActivity(settingIntent);
                        break;
                    case R.id.ll_add_recommendDish:
                        Intent uploadIntent = new Intent(
                                RecommendDishActivity.this,
                                UploadPhotoActivity.class);
                        uploadIntent.putExtra("businessId", businessId);
                        startActivity(uploadIntent);
                        break;

                    default:
                        break;
                }
            }
        };
        this.addRecommendDish.setOnClickListener(recommendListClick);
        this.recDishListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> itemView, View arg1,
                    int parameInt, long arg3) {
                RecDishEntity rde = RecommendDishActivity.this.recDishList
                        .get(parameInt);
                Intent dishDetailIntent = new Intent(RecommendDishActivity.this,
                        RecommendDishDetailActivity.class);
                dishDetailIntent.putExtra("recDishInfo", rde);
                startActivity(dishDetailIntent);
            }
        });
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        Intent intent = getIntent();
        businessId = intent.getStringExtra("businessId");
    }

    /**
     * 获取评论列表数据
     */
    private void requestDishListData() {
        if (StringUtil.isNullOrEmpty(businessId)) {
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> nearbyBusinessParamMap = new HashMap<String, Object>();
                        nearbyBusinessParamMap.put("sellerId", businessId);
                        nearbyBusinessParamMap.put("pageCnt", "1");
                        nearbyBusinessParamMap.put("pageRow", "20");
                        nearbyBusinessParamMap.put("sort", "1");
                        nearbyBusinessParamMap.put("language", commonLanguage);
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                RecommendDishActivity.this,
                                Constants.SELLER_REC_DISH_LIST_API,
                                nearbyBusinessParamMap);
                        if (commentItemList != null) {
                            recDishList = new ParseJson()
                                    .parseBusinessRecDishListJson(commentItemList);
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        setDishListAdapter();
                    }

                });
        ald.execute(0);
    }

    /**
     * 调用菜单列表适配器
     */
    public void setDishListAdapter() {
        if (CollectionUtil.UnNullAndZero(recDishList)) {
            this.recDishListAdapter = new RecDishListAdapter(
                    RecommendDishActivity.this);
            this.recDishListAdapter.addItems(recDishList);
            this.recDishListView.setAdapter(this.recDishListAdapter);
        }
    }
}
